package cn.itsource.basic.product.service.impl;

import cn.itsource.basic.service.impl.BaiduAiAuditServiceImpl;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.product.domain.Product;
import cn.itsource.product.domain.ProductDetail;
import cn.itsource.product.domain.ProductOnlineAuditLog;
import cn.itsource.product.mapper.ProductDetailMapper;
import cn.itsource.product.mapper.ProductMapper;
import cn.itsource.product.mapper.ProductOnlineAuditLogMapper;
import cn.itsource.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {


    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private BaiduAiAuditServiceImpl baiduAiAuditService;

    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;


    @Override
    @Transactional
    public void add(Product product) {
        super.add(product);

        ProductDetail productDetail = product.getProductDetail();
        if (productDetail!=null){

            productDetail.setProductId(product.getId());
            productDetailMapper.add(productDetail);
        }
    }


    @Override
    @Transactional
    public void update(Product product) {
        super.update(product);
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail!=null){

            productDetail.setProductId(product.getId());
            productDetailMapper.update(productDetail);
        }




    }

    @Override
    @Transactional
    public void delete(Long id) {
        super.delete(id);
        productDetailMapper.deleteByProductId(id);
    }

    @Override
    @Transactional
    public void batchDelete(List<Long> ids) {
        super.batchDelete(ids);

        for (Long id : ids) {
            delete(id);
        }
    }


    //上架
    @Override
    public AjaxResult onsale(List<Long> ids) {

        for (Long id : ids) {

            Product product = productMapper.findById(id);
            //审核图片的类容
            Boolean boot1 = baiduAiAuditService.textAudit(product.getName());
            Boolean boot2 = baiduAiAuditService.imageAudit(product.getResources());
            
            //添加productOnlineAuditLog表中的数据
            ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
            productOnlineAuditLog.setProductId(product.getId());
            productOnlineAuditLog.setAuditTime(new Date());
            
            if (boot1 && boot2){

                //更改状态，和上架时间
                Date onsaletime = new Date();
                productMapper.onsale(onsaletime,id);

                //添加productOnlineAuditLog表中的数据
                productOnlineAuditLog.setState(1);
                productOnlineAuditLog.setNote("审核成功");
                productOnlineAuditLogMapper.add(productOnlineAuditLog);
            }else {

                productOnlineAuditLog.setState(0);
                productOnlineAuditLog.setNote("审核失败");
                productOnlineAuditLogMapper.add(productOnlineAuditLog);
                return AjaxResult.me().setSuccess(false).setMessage("审核不通过");
            }
        }

        return new AjaxResult();
    }

    //下架
    @Override
    public AjaxResult offsale(List<Long> ids) {

        //下架时间
        Date offsaletime = new Date();

        productMapper.offsale(offsaletime,ids);

        return new AjaxResult();
    }
}
